package com.zhangxin.aiInterview.controller;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhangxin.aiInterview.annotation.AuthCheck;
import com.zhangxin.aiInterview.common.BaseResponse;
import com.zhangxin.aiInterview.common.ErrorCode;
import com.zhangxin.aiInterview.common.ResultUtils;
import com.zhangxin.aiInterview.exception.BusinessException;
import com.zhangxin.aiInterview.model.dto.position.InsertCompanyAndTechFieldAndPositionRequest;
import com.zhangxin.aiInterview.model.entity.Company;
import com.zhangxin.aiInterview.model.entity.Position;
import com.zhangxin.aiInterview.model.entity.TechField;
import com.zhangxin.aiInterview.model.vo.response.GetTechFieldAndPositionResponse;
import com.zhangxin.aiInterview.service.CompanyService;
import com.zhangxin.aiInterview.service.PositionService;
import com.zhangxin.aiInterview.service.TechFieldService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 岗位相关接口
 */
@RestController
@RequestMapping("/position")
@Slf4j
public class PositionController {
    @Resource
    private PositionService positionService;
    @Resource
    private TechFieldService techFieldService;
    @Resource
    private CompanyService companyService;


    /**
     * 获取技术领域和岗位
     *
     * @return
     */
    @GetMapping("/get/techFieldAndPosition")
    public BaseResponse<GetTechFieldAndPositionResponse> getTechFieldAndPosition() {
        List<TechField> techFieldList = techFieldService.list();
        List<Position> positionList = positionService.list();
        GetTechFieldAndPositionResponse getTechFieldAndPositionResponse = new GetTechFieldAndPositionResponse();
        getTechFieldAndPositionResponse.setTechFieldList(techFieldService.getTechFieldVO(techFieldList));
        getTechFieldAndPositionResponse.setPositionList(positionService.getPositionVO(positionList));
        return ResultUtils.success(getTechFieldAndPositionResponse);
    }

    /**
     * 获取公司列表
     *
     * @return
     */
    @GetMapping("/get/companyList")
    public BaseResponse<List<Company>> getCompanyList() {
        List<Company> companyList = companyService.list();
        return ResultUtils.success(companyList);
    }

    /**
     * 获取技术领域列表
     *
     * @return
     */
    @GetMapping("/get/techFieldList")
    public BaseResponse<List<TechField>> getTechFieldList() {
        List<TechField> techFieldList = techFieldService.list();
        return ResultUtils.success(techFieldList);
    }

    /**
     * 根据技术领域id和公司id获取岗位列表
     * @param techFieldId
     * @param companyId
     * @return
     */
    @GetMapping("/get/positionList")
    public BaseResponse<List<Position>> getPositionList(@RequestParam("techFieldId") Long techFieldId, @RequestParam("companyId") Long companyId) {
        List<Position> positionList = positionService.list(
                new QueryWrapper<Position>()
                        .eq("techFieldId", techFieldId)
                        .eq("companyId", companyId)
        );
        return ResultUtils.success(positionList);
    }

    /**
     * 新增技术领域
     * @param techFieldName
     * @return
     */
    @GetMapping("/insert/techField")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> insertTechField(String techFieldName){
        //查询是否已存在
        TechField techField = techFieldService.lambdaQuery()
                .eq(TechField::getName, techFieldName)
                .one();
        if(ObjUtil.isNotEmpty(techField)) throw new BusinessException(ErrorCode.PARAMS_ERROR, "技术领域已存在");

        //新增
        TechField techField1 = new TechField();
        techField1.setName(techFieldName);
        boolean save = techFieldService.save(techField1);
        return ResultUtils.success(save);
    }

    /**
     * 新增公司
     */
    @GetMapping("/insert/company")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> insertCompany(String companyName){
        //查询是否已存在
        Company company = companyService.lambdaQuery()
                .eq(Company::getName, companyName)
                .one();
        if(ObjUtil.isNotEmpty(company)) throw new BusinessException(ErrorCode.PARAMS_ERROR, "公司已存在");

        //新增
        Company company1 = new Company();
        company1.setName(companyName);
        boolean save = companyService.save(company1);
        return ResultUtils.success(save);
    }

    /**
     * 新增公司岗位
     * @param insertCompanyAndTechFieldAndPositionRequest
     * @return
     */
    @PostMapping("/insert/companyAndTechFieldAndPosition")
//    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> insertCompanyAndTechFieldAndPosition(@RequestBody InsertCompanyAndTechFieldAndPositionRequest insertCompanyAndTechFieldAndPositionRequest){
        String techFieldName = insertCompanyAndTechFieldAndPositionRequest.getTechFieldName();
        String companyName = insertCompanyAndTechFieldAndPositionRequest.getCompanyName();
        String positionName = insertCompanyAndTechFieldAndPositionRequest.getPositionName();
        //查询公司是否存在
        Company company = companyService.lambdaQuery()
                .eq(Company::getName, companyName)
                .one();
        if(ObjUtil.isEmpty(company)) throw new BusinessException(ErrorCode.PARAMS_ERROR, "公司不存在，请先创建公司");
        //查询技术领域是否存在
        TechField techField = techFieldService.lambdaQuery()
                .eq(TechField::getName, techFieldName)
                .one();
        if(ObjUtil.isEmpty(techField)) throw new BusinessException(ErrorCode.PARAMS_ERROR, "技术领域不存在，请先创建技术领域");
        //新增公司岗位
        Position position = new Position();
        position.setName(positionName);
        position.setTechFieldId(techField.getId());
        position.setTechFieldName(techField.getName());
        position.setCompanyId(company.getId());
        position.setCompanyName(company.getName());
        boolean save = positionService.save(position);
        return ResultUtils.success(save);
    }

    /**
     * 获取所有公司岗位
     * @return
     */
    @GetMapping("/get/all/positionList")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<List<Position>> getAllPositionList(){
        return ResultUtils.success(positionService.list());
    }

    /**
     * 获取指定公司的岗位
     * @return
     */
    @GetMapping("/get/company/positionList")
    @AuthCheck(mustRole = "recruiter")
    public BaseResponse<List<Position>> getCompanyPositionList(String companyName){
        if(StrUtil.isBlank(companyName)) throw new BusinessException(ErrorCode.PARAMS_ERROR, "公司名称不能为空");
        List<Position> positionList = positionService.lambdaQuery()
                .eq(Position::getCompanyName, companyName)
                .list();
        return ResultUtils.success(positionList);
    }
}
